package org.ucas.faker.web.utils;

import java.io.*;

public class CloneUtils {

  @SuppressWarnings("unchecked")
  public static <T> T clone(T obj) {
    if(! (obj instanceof Serializable)) return null;
    try {
      Serializable tmp = (Serializable) obj;
      ByteArrayOutputStream bs = new ByteArrayOutputStream();
      ObjectOutputStream os = new ObjectOutputStream(bs);
      os.writeObject(tmp);

      ByteArrayInputStream ibs = new ByteArrayInputStream(bs.toByteArray());
      ObjectInputStream is = new ObjectInputStream(ibs);
      T res = (T) is.readObject();
      return res;
    } catch (Exception e) {
      return null;
    }

  }
}
